﻿using System;
using System.Collections.Generic;
using Model;
using EFEntity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Data.SqlClient;
using System.Data;
using System.Threading.Tasks;
using System.Linq;
using Iservices;

namespace Services
{
    public class salary_standard_detailsService: Isalary_standard_detailsService
    {
        private readonly  MyDbContext myDbContext;

        public salary_standard_detailsService(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }

        public async Task<int> Add(salary_standard_details t)
        {
            Esalary_standard_details test = new Esalary_standard_details()
            {
                standard_id = t.standard_id,
                standard_name = t.standard_name,
                item_id =t.sdt_id ,
                item_name=t.item_name,
                salary=t.salary
            };
            myDbContext.salary_standard_details.Add(test);
            return await myDbContext.SaveChangesAsync();
        }

        public async Task<int> Delete(int id)
        {
            Esalary_standard_details test = new Esalary_standard_details()
            {
                sdt_id = id             
            };
            myDbContext.salary_standard_details.Attach(test);
            myDbContext.salary_standard_details.Remove(test);
            return await myDbContext.SaveChangesAsync();
        }

        public Task<List<salary_standard_details>> FenYe(FenYe fy)
        {
            throw new NotImplementedException();
        }

        public async Task<List<salary_standard_details>> SelectAll()
        {
            List<Esalary_standard_details> list = new List<Esalary_standard_details>();
            await Task.Run(() =>
            {
                list = myDbContext.salary_standard_details.ToList();
            });

            List<salary_standard_details> list2 = new List<salary_standard_details>();
            foreach (Esalary_standard_details t in list)
            {
                salary_standard_details ck = new salary_standard_details()
                {
                    sdt_id = t.sdt_id,
                    standard_id = t.standard_id,
                    standard_name = t.standard_name,
                    item_id = t.item_id,
                    item_name = t.item_name,
                    salary = t.salary
                };
                list2.Add(ck);
            }
            return list2;
           
        }

        public Task<salary_standard_details> SelectID(int id)
        {
            throw new NotImplementedException();
        }

        public async Task<int> Update(salary_standard_details t)
        {
            Esalary_standard_details e = new Esalary_standard_details()
            {
                sdt_id = t.sdt_id,
                standard_id = t.standard_id,
                standard_name = t.standard_name,
                item_id = t.item_id,
                item_name = t.item_name,
                salary = t.salary
            };
            myDbContext.salary_standard_details.Find(e);
            myDbContext.Entry(e).State = EntityState.Modified;
            return await myDbContext.SaveChangesAsync();

        }
    }
}
